<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no">
    <title>FrozenUI Demo</title>
    <link rel="stylesheet" href="static/frozenui-1.3.0/css/frozen.css">
</head>
<body style="margin:0;background:url('http://img05.tooopen.com/images/20140326/sy_57640132134.jpg') no-repeat;background-size:cover;">
<section>
    <div id="msgShow">

    </div>
    <input type="text" id="content">

    <input type="hidden" id="room" value="vip房01">
    <input type="hidden" id="race" value="944021774331416576">
    <input type="hidden" id="fromUser" value="1">
    <button onclick="wsSend()">发送</button><br>
    <a href="/vip2.jsp">vip2</a>

</section>
<script src="/static/frozenui-1.3.0/lib/zepto.min.js"></script>
<script src="/static/frozenui-1.3.0/js/frozen.js"></script>
<script src="/static/frozenui-1.3.0/lib/sockjs.js"></script>
<script>
    window.onbeforeunload=onclose;
    function onclose()
    {
        var Sys = {};
        var warnning = '关闭';
        var ua = navigator.userAgent.toLowerCase();
        if (window.ActiveXObject)
            Sys.ie = ua.match(/msie ([\d.]+)/)[1]
        else if (document.getBoxObjectFor)
            Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
        if(Sys.ie) {//for IE
            if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
            {
                window.event.returnValue = warnning ;
            }
        }
        if(Sys.firefox) //for FF
            return warnning;
    }
    var websocket;
    // 首先判断是否 支持 WebSocket
    if('WebSocket' in window) {
        websocket = new WebSocket("ws://192.168.1.88:8080/wsChat?roomId=212&race=322543421&type=1");
    }else {
        websocket = new SockJS("http://192.168.1.88:8080/wsChat?roomId=212&race=322543421&type=1");
    }
    websocket.onopen = function (event) {
        console.log(event);
//        var content = $("#content").val();
        var now = getNowFormatDate();
        var fromUser = $("#fromUser").val();
        var room = $("#room").val();
        var race = $("#race").val();
        var wsUser = {"userId":"956352151611904000","userName":"18750801406","roomId":"212","roomName":"英雄联盟","raceId":"322543421","raceName":"lpl","content":"测试","msgType":"-1"};
        var wsUserStr = JSON.stringify(wsUser);
        websocket.send(wsUserStr);
    };
    websocket.onmessage = function (event) {
      console.log(event);
    }
    websocket.onclose = function (event) {
        //websocket.close()
    }
    function wsSend() {
        var content = $("#content").val();
        var now = getNowFormatDate();
        var fromUser = $("#fromUser").val();
        var room = $("#room").val();
        var race = $("#race").val();
        if(content){
            var wsUser = {"userId":"956352151611904000","userName":"18750801406","roomId":"212","roomName":"英雄联盟","raceId":"322543421","raceName":"lpl","content":"测试","msgType":"-1"};
            //var wsUser2 = {"fromUser":'w ong ge',"msgType":-1,"msgTo":room,"content":"测试1","sendTime":now,"race":race};

            var wsUserStr = JSON.stringify(wsUser);
            websocket.send(wsUserStr);
        }
    }

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentDate= date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        return currentDate;
    }

</script>
</body>
</html>